package com.bootdo.essential.serivce.impl;

import static com.bootdo.common.utils.ShiroUtils.getUserId;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.bootdo.common.utils.DateUtils;
import com.bootdo.common.utils.StringUtils;
import com.bootdo.essential.dao.BankDao;
import com.bootdo.essential.domain.BankDO;
import com.bootdo.essential.serivce.BankService;
import com.bootdo.system.dao.UserRoleDao;

@Service
public class BankServiceImpl implements BankService {

	@Autowired
	private BankDao bankMapper;
	@Autowired
	private UserRoleDao userRoleMapper;
	
	@Override
	public BankDO get(Long id){
		return bankMapper.get(id);
	}

	@Override
	public BankDO getByUser(Long userId) {
		return bankMapper.getByUser(userId);
	}
	
	@Override
	public List<BankDO> list(Map<String, Object> map){
		//格式话日期
		String startDate = (String) map.get("startTime");
		String endDate = (String) map.get("endTime");
		if (StringUtils.isNotBlank(startDate)){
			startDate = DateUtils.getDateOfFormat(startDate, "yyyy-MM-dd") + " 00:00:00";
			endDate = DateUtils.getDateOfFormat(endDate, "yyyy-MM-dd") + " 23:59:59";
			map.put("startTime", startDate);
			map.put("endTime", endDate);
		}

		//获取用户权限
		int checkUserIsManager = userRoleMapper.checkUserIsManager(getUserId());
		if (checkUserIsManager != 1){
			map.put("userId", getUserId());
		}
		List<BankDO> list = bankMapper.list(map);
		return list;
	}
	
	@Override
	public int count(Map<String, Object> map){
		return bankMapper.count(map);
	}
	
	@Override
	@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,timeout=36000,rollbackFor=Exception.class)
	public int save(BankDO bank){
		BankDO banks = bankMapper.getByUser(getUserId());
		if (banks != null){
			banks.setStatus(2);
			bankMapper.update(banks);
		}

		bank.setUserId(getUserId());
		bank.setStatus(1);
		bank.setGmtCreate(new Date());
		bank.setGmtModified(new Date());
		return bankMapper.save(bank);
	}
	
	@Override
	@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,timeout=36000,rollbackFor=Exception.class)
	public int update(BankDO bank){
		BankDO banks = bankMapper.get(bank.getId());
		bank.setGmtModified(new Date());
		bank.setStatus(2);
		int count = bankMapper.update(bank);
		if (count > 0) {
			bank.setStatus(1);
			bank.setUserId(banks.getUserId());
			bank.setGmtCreate(new Date());
			bank.setGmtModified(new Date());
			count = bankMapper.save(bank);
		}
		return count;
	}
	
	@Override
	@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,timeout=36000,rollbackFor=Exception.class)
	public int remove(Long id){
		return bankMapper.remove(id);
	}
	
	@Override
	@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,timeout=36000,rollbackFor=Exception.class)
	public int batchRemove(Long[] ids){
		return bankMapper.batchRemove(ids);
	}
	
}
